

package GUI;

import java.awt.Container;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;


public class PasswordChange extends javax.swing.JFrame {
    /** Creates new form Login */
    public MainScreen main;
    private Container parent;
    public PasswordChange(Container parent) {
        initComponents();
        setLocationRelativeTo(null);   
        this.parent = parent;
        txt_TaiKhoan.setText(MainScreen.lbAccount.getText());
        windownClose();
    }
    public void windownClose(){
        addWindowListener(new WindowAdapter() {
             public void windowClosing(WindowEvent e) {      
                 PasswordChange.this.parent.setEnabled(true);      
                 PasswordChange.this.parent.requestFocus();        
                 PasswordChange.this.dispose();
            }
    });
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        txt_TaiKhoan = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtPassCurrent = new javax.swing.JPasswordField();
        btn_Exit1 = new javax.swing.JButton();
        btn_Login = new javax.swing.JButton();
        txtPassCon = new javax.swing.JPasswordField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txtPassNew = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Quản Lý Internet");
        setBackground(new java.awt.Color(255, 255, 255));
        setName("Login"); // NOI18N
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(227, 239, 249));
        jPanel1.setForeground(new java.awt.Color(255, 255, 255));
        jPanel1.setLayout(new java.awt.BorderLayout());
        jPanel1.add(jSeparator1, java.awt.BorderLayout.CENTER);

        jLabel1.setBackground(new java.awt.Color(255, 204, 204));
        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 0, 204));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Thay đổi mật khẩu");
        jPanel1.add(jLabel1, java.awt.BorderLayout.PAGE_START);

        jPanel3.setBackground(new java.awt.Color(227, 239, 249));
        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Tài Khoản", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Times New Roman", 1, 16), new java.awt.Color(102, 102, 0))); // NOI18N
        jPanel3.setForeground(new java.awt.Color(255, 255, 255));

        jLabel5.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(102, 102, 0));
        jLabel5.setText("Tên Tài khoản : ");

        txt_TaiKhoan.setEditable(false);
        txt_TaiKhoan.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        txt_TaiKhoan.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        txt_TaiKhoan.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txt_TaiKhoanMouseClicked(evt);
            }
        });
        txt_TaiKhoan.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txt_TaiKhoanKeyPressed(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(102, 102, 0));
        jLabel6.setText("Mật khẩu hiện tại: ");

        txtPassCurrent.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        txtPassCurrent.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtPassCurrentMouseClicked(evt);
            }
        });
        txtPassCurrent.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtPassCurrentKeyPressed(evt);
            }
        });

        btn_Exit1.setFont(new java.awt.Font("Times New Roman", 1, 13)); // NOI18N
        btn_Exit1.setForeground(new java.awt.Color(204, 0, 0));
        btn_Exit1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Exit_22.png"))); // NOI18N
        btn_Exit1.setText("Đóng");
        btn_Exit1.setToolTipText("");
        btn_Exit1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_Exit1ActionPerformed(evt);
            }
        });

        btn_Login.setFont(new java.awt.Font("Times New Roman", 1, 13)); // NOI18N
        btn_Login.setForeground(new java.awt.Color(0, 0, 255));
        btn_Login.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Account.png"))); // NOI18N
        btn_Login.setText("Đổi mật khẩu");
        btn_Login.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_LoginActionPerformed(evt);
            }
        });

        txtPassCon.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        txtPassCon.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtPassConMouseClicked(evt);
            }
        });
        txtPassCon.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtPassConKeyPressed(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel7.setForeground(new java.awt.Color(102, 102, 0));
        jLabel7.setText("Xác nhận mật khẩu : ");

        jLabel8.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel8.setForeground(new java.awt.Color(102, 102, 0));
        jLabel8.setText("Mật khẩu mới: ");

        txtPassNew.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        txtPassNew.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtPassNewMouseClicked(evt);
            }
        });
        txtPassNew.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtPassNewKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(40, 40, 40)
                        .addComponent(btn_Login)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btn_Exit1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(33, 33, 33))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txt_TaiKhoan)
                            .addComponent(txtPassCurrent, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txtPassCon)))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 41, Short.MAX_VALUE)
                        .addComponent(txtPassNew, javax.swing.GroupLayout.PREFERRED_SIZE, 212, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txt_TaiKhoan, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtPassCurrent, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addGap(20, 20, 20)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(txtPassNew, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtPassCon, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_Login, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_Exit1, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        jPanel1.add(jPanel3, java.awt.BorderLayout.PAGE_END);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(28, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtPassConKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPassConKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtPassConKeyPressed

    private void txtPassConMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtPassConMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_txtPassConMouseClicked

    @SuppressWarnings("SuspiciousIndentAfterControlStatement")
    private void btn_LoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_LoginActionPerformed
                if(txtPassCurrent.getText().equals("")||txtPassNew.getText().equals("")||txtPassCon.getText().equals("")){
                    JOptionPane.showMessageDialog(null,"Nhập đầy đủ thông tin");
                    if(txtPassCon.getText().equals("")){
                        txtPassCon.requestFocus();
                    }
                    else if(txtPassNew.getText().equals("")){
                        txtPassNew.requestFocus();
                    }
                    else
                        txtPassCurrent.requestFocus();
                }
                else
                    if(!txtPassNew.getText().equals(txtPassCon.getText())){
                       JOptionPane.showMessageDialog(null,"Mật khẩu xác nhận không khớp");
                       txtPassCon.setText("");
                       txtPassCon.requestFocus();
                    }
                    else{
            try {
                ResultSet rs=Login.dtConnect.retriveData("select * from employee where employeename = '"+txt_TaiKhoan.getText()+"' and password ='"+txtPassCurrent.getText()+"'");
                if(rs.next()){
                    Login.dtConnect.updateData("UPDATE employee SET PASSWORD = '"+txtPassNew.getText()+"' WHERE EMPLOYEENAME='"+txt_TaiKhoan.getText()+"'");
                    JOptionPane.showMessageDialog(null, "Đổi mật khẩu thành công");
                    parent.setEnabled(true);
                    this.dispose();
                }
                else
                    JOptionPane.showMessageDialog(null, "Sai mật khẩu");
                    txtPassCurrent.setText("");
                    txtPassCurrent.requestFocus();
            } catch (SQLException ex) {
                Logger.getLogger(PasswordChange.class.getName()).log(Level.SEVERE, null, ex);
            }
                    }
                
    }//GEN-LAST:event_btn_LoginActionPerformed

    private void btn_Exit1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_Exit1ActionPerformed
        // TODO add your handling code here:
        parent.setEnabled(true);
        this.dispose();
    }//GEN-LAST:event_btn_Exit1ActionPerformed

    private void txtPassCurrentKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPassCurrentKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtPassCurrentKeyPressed

    private void txtPassCurrentMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtPassCurrentMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_txtPassCurrentMouseClicked

    private void txt_TaiKhoanKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_TaiKhoanKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_txt_TaiKhoanKeyPressed

    private void txt_TaiKhoanMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txt_TaiKhoanMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_txt_TaiKhoanMouseClicked

    private void txtPassNewMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtPassNewMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_txtPassNewMouseClicked

    private void txtPassNewKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPassNewKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtPassNewKeyPressed
            
    /**
     * @param args the command line arguments
     */
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_Exit1;
    private javax.swing.JButton btn_Login;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JPasswordField txtPassCon;
    private javax.swing.JPasswordField txtPassCurrent;
    private javax.swing.JPasswordField txtPassNew;
    private javax.swing.JTextField txt_TaiKhoan;
    // End of variables declaration//GEN-END:variables
    
}
